package com.loan.web.pdf.controller;

import com.loan.common.annotation.Log;
import com.loan.common.core.controller.BaseController;
import com.loan.common.core.domain.AjaxResult;
import com.loan.common.core.page.TableDataInfo;
import com.loan.common.enums.BusinessType;
import com.loan.common.utils.SecurityUtils;
import com.loan.common.utils.poi.ExcelUtil;
import com.loan.system.domain.AssistFile;
import com.loan.system.service.IAssistFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文件记录Controller
 *
 * @author Sophia
 * @date 2024-02-01
 */
@RestController
@RequestMapping("/system/file")
public class AssistFileController extends BaseController {
    @Autowired
    private IAssistFileService assistFileService;

    /**
     * 查询文件记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(AssistFile assistFile) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        assistFile.setUserId(userId);
        startPage();
        List<AssistFile> list = assistFileService.selectAssistFileList(assistFile);
        return getDataTable(list);
    }

    /**
     * 导出文件记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:export')")
    @Log(title = "文件记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AssistFile assistFile) {
        List<AssistFile> list = assistFileService.selectAssistFileList(assistFile);
        ExcelUtil<AssistFile> util = new ExcelUtil<AssistFile>(AssistFile.class);
        util.exportExcel(response, list, "文件记录数据");
    }

    /**
     * 获取文件记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:file:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(assistFileService.selectAssistFileById(id));
    }

    /**
     * 新增文件记录
     */
    @PreAuthorize("@ss.hasPermi('system:file:add')")
    @Log(title = "文件记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AssistFile assistFile) {
        return toAjax(assistFileService.insertAssistFile(assistFile));
    }

    /**
     * 修改文件记录
     */
    @PreAuthorize("@ss.hasPermi('system:file:edit')")
    @Log(title = "文件记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AssistFile assistFile) {
        return toAjax(assistFileService.updateAssistFile(assistFile));
    }

    /**
     * 删除文件记录
     */
    @PreAuthorize("@ss.hasPermi('system:file:remove')")
    @Log(title = "文件记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(assistFileService.deleteAssistFileByIds(ids));
    }
}
